package us.gaaoc.framework.webapp.controllers;


import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.servlet.http.HttpServletRequest;

import us.gaaoc.framework.model.CaseRecord;
import us.gaaoc.framework.model.Court;
import us.gaaoc.framework.model.CourtEventType;
import us.gaaoc.framework.model.CourtEvents;
import us.gaaoc.framework.model.Person;
import us.gaaoc.framework.model.constants.Constants;
import us.gaaoc.framework.model.events.court.CourtEventsExtendedData;
import us.gaaoc.framework.model.events.court.CourtEventsExtendedType;
import us.gaaoc.framework.util.DateUtil;
import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;
import us.gaaoc.framework.webapp.controllers.model.CourtDateField;
import us.gaaoc.framework.webapp.util.Messages;

/**
 * This is the update Court Date controller for setting of a court date with
 * extended functionality (Different courts need different data)
 * @author artripa
 *
 */
public class UpdateCourtDateController extends BaseBackingBean  {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Person currentUser;
	private String currentCourtUuid;
	private CaseRecord currentCase;
	private Date proposedCourtDate;
	
	List<CourtDateField> fields;
	private Date currentCourtDate;
	
	public List<CourtDateField> getFields() {
		return fields;
	}


	public void setFields(List<CourtDateField> fields) {
		this.fields = fields;
	}


	@SuppressWarnings("unchecked")
	public Map<String, Object> getSessionMap() {
		Map<String, Object> sessionMap = this.getFacesContext().getExternalContext()
			.getSessionMap();
		
		return sessionMap;
	}

	
	public UpdateCourtDateController()
	{
		currentCourtUuid = (String) this.getSessionMap().get("courtUuid");
		currentUser = (Person) this.getSessionMap().get("person");
		String caseUuid = (String) this.getSessionMap().get("caseUuid");
		currentCase = this.getModelFacade().findCaseRecordById(caseUuid,currentCourtUuid);
		setCurrentCourtDate(this.getModelFacade().findCourtDate(caseUuid));
		fields = setupFields();
	}
	
	
	public String submit()
	{
		
		
		/*
		for(CourtDateField f : this.getFields())
		{
			if(f.getId().equalsIgnoreCase("courtroom"))
			{
				strCourtRoom = f.getValue();
			}
		}
		*/	
					
		// Then we update the date/time
		this.getModelFacade().updateCourtDate(this.getCurrentCase().getUuid(), this.proposedCourtDate, this.currentUser.getUuid());			
		
		/*
		if(currentCourtUuid.equalsIgnoreCase(Constants.Courts.))
		{					

			// setup court room
			
			if(strCourtRoom.equalsIgnoreCase(""))
			{
				//Validation Error
				return "";
			}else{		
				
				this.getModelFacade().updateCourtDateExtendedData(						
						this.getCurrentCase().getUuid(), 
						Constants.CourtEventsExtendedType.CourtRoom,
						strCourtRoom);											
			}
		}
		*/
		
		return "done";
	}
	
	
	protected List<CourtDateField> setupFields()
	{
		List<CourtDateField> ret = new java.util.LinkedList<CourtDateField>();

		return ret;
	}


	public void setProposedCourtDate(Date proposedCourtDate) {
		this.proposedCourtDate = proposedCourtDate;
	}


	public Date getProposedCourtDate() {
		return proposedCourtDate;
	}


	public void setCurrentCase(CaseRecord currentCase) {
		this.currentCase = currentCase;
	}


	public CaseRecord getCurrentCase() {
		return currentCase;
	}
	
	
	
	protected boolean isValidDate(java.util.Date value) throws Exception {
		boolean validField = false;
		if ((value.compareTo(DateUtil.getCurrentDate()) <= 0) ) {
			validField = false;
			message = Messages.getMessage(
					"us.gaaoc.framework.webapp.util.errorMessage",
					"incorrectDate", null);
			message.setDetail(message.getDetail());
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
		} else {
			Calendar cal = new GregorianCalendar();
			cal.setTime(value);
			int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
			if(dayOfWeek == 1 || dayOfWeek == 7) {
				validField = false;
				message = Messages.getMessage(
						"us.gaaoc.framework.webapp.util.errorMessage",
						"incorrectDate", null);
				message.setDetail(message.getDetail());
				message.setSeverity(FacesMessage.SEVERITY_ERROR);
			}else {
				validField = true;
			}
		}
		return validField;
	}
	
	
	
	private FacesMessage message;
	
	public FacesMessage getMessage() {
		return message;
	}


	public void setMessage(FacesMessage message) {
		this.message = message;
	}

	
	
	

	public void validateDate(FacesContext context, UIComponent component,
			Object value) throws ValidatorException, Exception {
		java.util.Date newValue = (java.util.Date) value;

		/*
		 * There is a bug in jsf page to pass over a date value, the passed
		 * value is always 1 day behind . The day field of the Calendar object
		 * starts with 1 but the month field starts with 0. This confusion might
		 * be the cause of the bug.
		 */
		if (!isValidDate(newValue)) {
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(message);
		}
		Calendar c = new GregorianCalendar();
		c.setTime(newValue);

		//System.out.println("Month = " + c.get(Calendar.MONTH));
		//System.out.println("Date = " + c.get(Calendar.DATE));

		int date = c.get(Calendar.DATE);
		c.set(Calendar.DATE, date + 1);
		newValue = c.getTime();

		
	}


	public void setCurrentCourtDate(Date currentCourtDate) {
		this.currentCourtDate = currentCourtDate;
	}


	public Date getCurrentCourtDate() {
		return currentCourtDate;
	}

	
	
	

}
